package cur.nature.framework.core.util;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.lang.reflect.Type;
import java.util.List;

public class JsonUtil {

    public static <T> T copyObj(Object source, Class<T> trarget) {
        T parseObject = parseObject(JsonUtil.toJSONString(source), trarget);
        return parseObject;
    }

    public static <T> List<T> copyList(List source, Class<T> trarget) {
        List<T> parseObject = parseArray(JsonUtil.toJSONString(source), trarget);
        return parseObject;
    }

    public static Object parseSpecObject(String text) {
        if (StringUtil.isNotEmpty(text) || text.trim().startsWith("[")){
            return parseArray(text);
        }
        return JSON.parseObject(text);
    }

    public static JSONObject parseObject(String text) {
        return JSON.parseObject(text);
    }

    public static <T> T parseObject(String text, Class<T> clazz) {
        return JSON.parseObject(text, clazz);
    }

    public static String toJSONString(Object object) {
        return JSON.toJSONString(object);
    }

    public static String toJSONString(Object object, com.alibaba.fastjson2.JSONWriter.Feature... features){
        return JSON.toJSONString(object, features);
    }

    public static JSONArray parseArray(String text) {
        return JSON.parseArray(text);
    }

    public static <T> List<T> parseArray(String text, Type type){
        return JSON.parseArray(text, type);
    }

    public static <T> List<T> parseArray(String text, Class<T> type){
        return JSON.parseArray(text, type);
    }
}
